package com.mao.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mao.common.lang.Result;
import com.mao.entity.Commont;
import com.mao.service.CommontService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Author: @524
 * @since 2022-05-13
 */
@RestController
@RequestMapping("/commont")
@Api(tags = "文章评论")
public class CommontController {
    @Autowired
    private CommontService commontService;
    @ApiOperation(value = "添加评论")
    @PostMapping("/")

    public Result addCommont(@RequestBody Commont commont){

        commont.setCommontTime(LocalDateTime.now());
        if(commontService.save(commont)){
            return  Result.succ("添加成功");
        }
        return  Result.fail("添加失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除评论")
    public  Result deleteCommon(@PathVariable("id") Integer id){
        if(commontService.removeById(id)){
            return  Result.succ("删除成功！");
        }
        return  Result.fail("删除失败");
    }
    @PostMapping("/commont/{blogid}")
    @ApiOperation(value = "根据文章id查询评论")
    public Result  findCommontByBlogId(@PathVariable("blogid") Integer blogid){

        QueryWrapper<Commont> queryWrapper = new QueryWrapper<>();
        List<Commont> commonList = commontService.list(queryWrapper.eq("blogid", blogid));
        System.out.println("请求了评论接口");
        return  Result.succ("200",commonList);
    }
}
